
export class MiniApi {
    public readonly isSupported: boolean = false
    constructor() {
        try {
            this.isSupported = (window as any)['wx'] != null
        } catch (err) {
            console.error(err)
        }
    }

    createCanvas() {
        if (this.isSupported) {
            return wx.createCanvas()
        }
    }
    createImage() {
        if (this.isSupported) {
            return wx.createImage()
        }
    }
    /** [string wx.loadFont(string path)](https://developers.weixin.qq.com/minigame/dev/api/render/font/wx.loadFont.html)
         *
         * 加载自定义字体文件 */
    loadFont(
        /** 字体文件路径。支持本地路径、代码包路径。 */
        path: string
    ): string | undefined {
        if (this.isSupported) {
            return wx.loadFont(path)
        }
        return undefined
    }
    onTouchStart(/** 开始触摸事件的监听函数 */
        listener: WechatMinigame.OnTouchStartCallback) {
        if (this.isSupported) {
            return wx.onTouchStart(listener)
        }
    }
    onTouchMove(/** 触点移动事件的监听函数 */
        listener: WechatMinigame.OnTouchMoveCallback) {
        if (this.isSupported) {
            return wx.onTouchMove(listener)
        }
    }
    onTouchEnd(/** 触摸结束事件的监听函数 */
        listener: WechatMinigame.OnTouchEndCallback) {
        if (this.isSupported) {
            return wx.onTouchEnd(listener)
        }
    }
    onTouchCancel(/** 触点失效事件的监听函数 */
        listener: WechatMinigame.OnTouchCancelCallback) {
        if (this.isSupported) {
            return wx.onTouchCancel(listener)
        }
    }
    getFileSystemManager() {
        if (this.isSupported) {
            return wx.getFileSystemManager()
        }
    }
    /** [Object wx.getSystemInfoSync()](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getSystemInfoSync.html)
*
* @deprecated 基础库版本 [2.20.1](https://developers.weixin.qq.com/miniprogram/dev/framework/compatibility.html) 起已废弃，请使用 [wx.getSystemSetting](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getSystemSetting.html)、[wx.getAppAuthorizeSetting](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getAppAuthorizeSetting.html)、[wx.getDeviceInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getDeviceInfo.html)、[wx.getWindowInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getWindowInfo.html)、[wx.getAppBaseInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getAppBaseInfo.html) 替换
*
* [wx.getSystemInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getSystemInfo.html) 的同步版本
*
* **示例代码**
*
* [在微信开发者工具中查看示例](https://developers.weixin.qq.com/s/WkUCgXmS7mqO)
*
* ```js
wx.getSystemInfo({
  success (res) {
    console.log(res.model)
    console.log(res.pixelRatio)
    console.log(res.windowWidth)
    console.log(res.windowHeight)
    console.log(res.language)
    console.log(res.version)
    console.log(res.platform)
  }
})
```
*
* ```js
try {
  const res = wx.getSystemInfoSync()
  console.log(res.model)
  console.log(res.pixelRatio)
  console.log(res.windowWidth)
  console.log(res.windowHeight)
  console.log(res.language)
  console.log(res.version)
  console.log(res.platform)
} catch (e) {
  // Do something when catch error
}
```
*
* ****
*
* ## 注意事项
* - 当 wx.getSystemInfoSync() 接口发生错误时可能会返回空对象
* - wx.getSystemInfo 接口由于会获取系统权限，可能触发授权弹窗，请使用 [wx.getSystemSetting](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getSystemSetting.html)、[wx.getAppAuthorizeSetting](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getAppAuthorizeSetting.html)、[wx.getDeviceInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getDeviceInfo.html)、[wx.getWindowInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getWindowInfo.html)、[wx.getAppBaseInfo](https://developers.weixin.qq.com/minigame/dev/api/base/system/wx.getAppBaseInfo.html) 替代 */
    getSystemInfoSync() {
        if (this.isSupported) {
            return wx.getSystemInfoSync()
        }
    }
    createInnerAudioContext(
        option?: WechatMinigame.CreateInnerAudioContextOption
    ): WechatMinigame.InnerAudioContext | null {
        if (this.isSupported) {
            return wx.createInnerAudioContext(option)
        }
        return null
    }
    setEnableDebug<T extends WechatMinigame.SetEnableDebugOption = WechatMinigame.SetEnableDebugOption>(
        option: T
    ): WechatMinigame.PromisifySuccessResult<T, WechatMinigame.SetEnableDebugOption> | undefined {
        if (this.isSupported) {
            return wx.setEnableDebug(option)
        }
        return undefined
    }
    notifyMiniProgramPlayableStatus(options: WechatMinigame.NotifyMiniProgramPlayableStatusOptions): void {
        if (this.isSupported) {
            return wx.notifyMiniProgramPlayableStatus(options)
        }
    }
}

/**
 * 适配微信试玩小游戏api
 */
export const minapi = new MiniApi()